home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18531 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!news
  2. From: Bradd W. Szonye <bradds@ix.netcom.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: RE: Printing fixed length char arrays in C++?
  5. Date: 20 Apr 1996 16:58:52 GMT
  6. Organization: Netcom
  7. Message-ID: <01bb2edb.180de560$65c2b7c7@Zany.localhost>
  8. References: <Pine.SGI.3.91.960418165036.21879A-100000@golgi> <4l83t7$l0@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: det-mi3-05.ix.netcom.com
  10. X-NETCOM-Date: Sat Apr 20 11:58:52 AM CDT 1996
  11. X-Newsreader: Microsoft Internet News
  12.  
  13.  
  14. On Friday, April 19, 1996, Blumenberg wrote...
  15. > Thanks for the reply.  Unfortunately the fixed length char arrays are in
  16. > structures in system-supplied C header files.  Converting them to C++
  17. > classes isn't worth the effort and would be a maintenance nightmare.
  18. > Thanks,
  19. > Dwayne
  20. Another solution would be to write a custom iostream manipulator.
  21. How to do this depends a little bit on how your compiler implements
  22. manipulators with parameters, but with a little research or browsing
  23. through your system headers you should be able to figure it out.
  24.  
  25. The syntax you would want is something like this:
  26. char s[10] = . . .
  27. cout << fixwidth(s, sizeof s) << endl;
  28.  
  29. and you'd write something like this:
  30. ostream& fixwidth(ostream& o, const char* s, size_t len)
  31. {
  32.     for (size_t i = 0; i < len; i++) {
  33.         if (s[i] == '\0') break; // optional
  34.         o << s[i];
  35.     }
  36. }
  37.  
  38. The optional line stops the write if it finds a null terminator inside the
  39. fixed-width string. Leave it in and the manipulator works like "strnxxx"
  40. functions, take it out and it works like "memxxx" functions.
  41.  
  42. The only thing left is how to hook the custom fixwidth into the iostreams
  43. system. How to do this is generally dependent on which compiler you use.
  44.  
  45.  
  46.